/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;


import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.StructuralFeature;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * 
 */
public class SlotDefiningStructuralFeatureTreeSelectionDialog extends Dialog {
	
	/**
	 * 
	 */
	protected Slot slot;
	
	/**
	 * 
	 */
	protected StructuralFeature definingSF;
	
	/**
	 * 
	 */
	protected Tree SFTree;
	
	
	/**
	 * 
	 * 
	 * @param slot 
	 * @param parent 
	 */
	public SlotDefiningStructuralFeatureTreeSelectionDialog (Shell parent, Slot slot) {
		super (parent);
		this.slot = slot;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	protected Control createDialogArea(Composite parent){
		Composite comp=(Composite)super.createDialogArea(parent);
		SFTree = new Tree(comp, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
		
		// build tree item for each classifier of the instance specification owing this slot
		Iterator<Classifier> iter = slot.getOwningInstance().getClassifiers().iterator();
		while(iter.hasNext()){
			Classifier currentClassifier = iter.next();
			TreeItem item = new TreeItem(SFTree, SWT.NONE);
			item.setText(currentClassifier.getName());
			item.setData(currentClassifier);
			item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentClassifier));
			buildSFTreeList(item, currentClassifier);
		}
			
		SFTree.setLayoutData(new GridData(300, 300));
		
		SFTree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = SFTree.getSelection();
				if(selection[0].getData() instanceof Property){
					definingSF = (StructuralFeature)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		
		return comp;
	}
	

	/**
	 * Returns the elements to import.
	 * 
	 * @return 
	 */
	public StructuralFeature getResult(){
		return definingSF;
	}
	
	/**
	 * This method builds the structural feature tree widget containing all structural features of the given classifier.
	 * 
	 * @param classifier 
	 * @param classifierTree 
	 */
	protected void buildSFTreeList(TreeItem classifierTree, Classifier classifier){
		Iterator iter = classifier.getAllAttributes().iterator();
		while(iter.hasNext()){
			StructuralFeature currentSF = (StructuralFeature) iter.next();
			if(isAvailableSF(currentSF)){
				TreeItem item = new TreeItem(classifierTree, SWT.SINGLE);
				item.setText(currentSF.getName()+" : "+currentSF.getType().getName());
				item.setData(currentSF);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentSF));
			}
		}
	}
	
	/**
	 * This method tests if the given structuralFeature is available for defining a slot or not.
	 * 
	 * @param SF 
	 * 
	 * @return 
	 */
	protected boolean isAvailableSF(StructuralFeature SF){
		boolean result = true;
		if(SF.getType()==null){
			return false;
		}
		Iterator<Slot> slots = slot.getOwningInstance().getSlots().iterator();
		while(slots.hasNext()){
			Slot currentSlot = slots.next();
			StructuralFeature definingFeature = currentSlot.getDefiningFeature();
			if(definingFeature!=null){
				if(definingFeature.equals(SF))
					return false;
			}
		}
		
		return result;
	}
	

}
